import { Request, Response } from 'express';
import ApiError from '@/utils/ApiError';
import catchAsync from '@/utils/catchAsync';
import { callDeepseekGrammar } from '@/services/grammar';

export class GrammarController {
  check = catchAsync(async (req: Request, res: Response) => {
    const { text } = req.body as { text?: string; lang?: string };
    if (!text || typeof text !== 'string' || !text.trim()) {
      throw new ApiError(400, 'text不能为空');
    }

    const apiKey = (process.env.DEEPSEEK_API_KEY || '').trim();
    if (!apiKey) {
      throw new ApiError(500, '服务未配置 DeepSeek API Key');
    }

    const result = await callDeepseekGrammar(text, apiKey);

    res.json({
      success: true,
      data: result,
    });
  });
}

export const grammarController = new GrammarController();